#ifndef __SE_CONFIGURATION_H__
#define __SE_CONFIGURATION_H__ 1

#include "json.hpp"
#include "Singleton.hpp"
#include <fstream>

/**
 * @brief 获取配置文件中的一个配置信息
 * 
 * @param key
 * @param type 配置类型的实际类型，如 std::string, int, double
 */
#define CONFIGURATION(key, type) se::Singleton<se::Configuration>::getInstance()[key].get<type>()

namespace se {

class Configuration final : Singleton<Configuration> {
    friend class Singleton<Configuration>;
public:
    /**
     * @brief 下标访问运算符
     * 
     * @param key 
     * @return nlohmann::json& 返回key对应的value. 使用 variable[key].get<valuetype>() 来获取实际的值
     */
    nlohmann::json &operator[](const char *key) {
        return this->_configuration[key];
    }

    /**
     * @brief Destroy the Configuration object. 同时将配置写入到配置文件中
     * 
     */
    ~Configuration() {
        std::ofstream configurationStream(this->_configurationPath);
        configurationStream << this->_configuration.dump(4);
        configurationStream.close();
    }

private:
    Configuration() {
        std::ifstream configurationStream(this->_configurationPath);
        configurationStream >> this->_configuration;
        configurationStream.close();
    }

private:
    nlohmann::json _configuration;
    std::string _configurationPath = "./config/configuration.json";  // 配置文件路径
};

}  // end of namespace se

#endif
